//
//  UpdatePhoneViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/19.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "UpdatePhoneViewController.h"

@interface UpdatePhoneViewController (){
    NSInteger count;
}
@property (weak, nonatomic) IBOutlet UITextField *textPhone;
@property (weak, nonatomic) IBOutlet UITextField *textCode;
@property (weak, nonatomic) IBOutlet UIButton *btnCode;

@end

@implementation UpdatePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    
    [self loadNav];
}

- (void)loadNav{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    [line makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(0.5);
    }];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"手机号修改";
    lab.textColor = [UIColor blackColor];
    lab.font = [UIFont systemFontOfSize:18.0];
    [view addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(34);
    }];
    
    //返回按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.left.equalTo(0);
        make.width.equalTo(40);
        make.height.equalTo(40);
    }];
}

- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)clickCode:(id)sender {
    if (_textPhone.text.length == 0) {
        [LICGToast showWithText:@"手机号不能为空"];
    }else if ([self validateMobile:_textPhone.text] == 0){
        [LICGToast showWithText:@"手机号格式不正确"];
    }else{
        [self performSelector:@selector(countClick) withObject:nil];
        _btnCode.enabled = NO;
        NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
        [parmas setObject:_textPhone.text forKey:@"mobile"];
        [parmas setObject:@"4" forKey:@"k"];
        NSLog(@"parmas:%@",parmas);
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSString *url = [NSString stringWithFormat:@"%@/App/Userlogin/CallSms",urlHead];
        [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"res:%@",responseObject);
            NSString *str = [responseObject objectForKey:@"status"];
            if ([str integerValue] == 200) {
                
            }else{
                
                [LICGToast showWithText:[responseObject objectForKey:@"msg"]];
            }
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"error:%@",error);
        }];
        
    }
    
}
//
-(void)countClick
{
    _btnCode.enabled = NO;
    count = 119;
    [_btnCode setTitle:@"119秒后重新获取" forState:UIControlStateNormal];
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}

- (void)timerFired:(NSTimer *)time{
    if (count !=1) {
        count -=1;
        
        NSString *str = [NSString stringWithFormat:@"%li",count];
        NSString *str2 = @"秒后重新获取";
        NSString *title = [NSString stringWithFormat:@"%@%@",str,str2];
        
        [_btnCode setTitle:title forState:UIControlStateNormal];
    }else{
        [time invalidate];//关闭定时器
        _btnCode.enabled = YES;
        [_btnCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
    
}
//修改手机号
- (IBAction)clickSave:(id)sender {
    if (_textPhone.text.length == 0) {
        [LICGToast showWithText:@"电话号码不能为空"];
        return;
    }else if ([self validateMobile:_textPhone.text] == 0){
        [LICGToast showWithText:@"手机号格式不正确"];
        return;
    }else if (_textCode.text.length == 0){
        [LICGToast showWithText:@"验证码不能为空"];
        return;
    }
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:_textPhone.text forKey:@"userphone"];
    [parmas setObject:_textCode.text forKey:@"smscode"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/editPhone",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        NSString *state = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        NSString *msg = [responseObject objectForKey:@"msg"];
        if ([state integerValue] == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        [LICGToast showWithText:msg];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
